package com.robotic.route.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.robotic.common.utils.DateUtils;
import com.robotic.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.robotic.route.mapper.InspectionRecordDetailsMapper;
import com.robotic.route.domain.InspectionRecordDetails;
import com.robotic.route.service.IInspectionRecordDetailsService;

/**
 * 巡检记录详情Service业务层处理
 *
 * @author tian
 * @date 2025-10-15
 */
@Service
public class InspectionRecordDetailsServiceImpl extends ServiceImpl<InspectionRecordDetailsMapper, InspectionRecordDetails> implements IInspectionRecordDetailsService
{
    @Autowired
    private InspectionRecordDetailsMapper inspectionRecordDetailsMapper;

    /**
     * 查询巡检记录详情
     *
     * @param id 巡检记录详情主键
     * @return 巡检记录详情
     */
    @Override
    public InspectionRecordDetails selectInspectionRecordDetailsById(Long id)
    {
        return inspectionRecordDetailsMapper.selectInspectionRecordDetailsById(id);
    }

    /**
     * 查询巡检记录详情列表
     *
     * @param inspectionRecordDetails 巡检记录详情
     * @return 巡检记录详情
     */
    @Override
    public List<InspectionRecordDetails> selectInspectionRecordDetailsList(InspectionRecordDetails inspectionRecordDetails)
    {
        return inspectionRecordDetailsMapper.selectInspectionRecordDetailsList(inspectionRecordDetails);
    }

    /**
     * 新增巡检记录详情
     *
     * @param inspectionRecordDetails 巡检记录详情
     * @return 结果
     */
    @Override
    public int insertInspectionRecordDetails(InspectionRecordDetails inspectionRecordDetails)
    {
        inspectionRecordDetails.setCreateTime(DateUtils.getNowDate());
        return inspectionRecordDetailsMapper.insertInspectionRecordDetails(inspectionRecordDetails);
    }

    /**
     * 修改巡检记录详情
     *
     * @param inspectionRecordDetails 巡检记录详情
     * @return 结果
     */
    @Override
    public int updateInspectionRecordDetails(InspectionRecordDetails inspectionRecordDetails)
    {
        inspectionRecordDetails.setUpdateTime(DateUtils.getNowDate());
        return inspectionRecordDetailsMapper.updateInspectionRecordDetails(inspectionRecordDetails);
    }

    /**
     * 批量删除巡检记录详情
     *
     * @param ids 需要删除的巡检记录详情主键
     * @return 结果
     */
    @Override
    public int deleteInspectionRecordDetailsByIds(Long[] ids)
    {
        return inspectionRecordDetailsMapper.deleteInspectionRecordDetailsByIds(ids);
    }

    /**
     * 删除巡检记录详情信息
     *
     * @param id 巡检记录详情主键
     * @return 结果
     */
    @Override
    public int deleteInspectionRecordDetailsById(Long id)
    {
        return inspectionRecordDetailsMapper.deleteInspectionRecordDetailsById(id);
    }

    @Override
    public Map<String, Object> getNum(InspectionRecordDetails inspectionRecordDetails) {
        List<InspectionRecordDetails> details = inspectionRecordDetailsMapper.selectInspectionRecordDetailsList(inspectionRecordDetails);
        Map<String, Long> countByAlarm =
                details.stream()
                        .collect(Collectors.groupingBy(InspectionRecordDetails::getAlarmType, Collectors.counting()));
        Map<String, Long> countByStatus =
                details.stream()
                        .collect(Collectors.groupingBy(InspectionRecordDetails::getStatus, Collectors.counting()));

        Map<String, Object> map = new HashMap<>();
        map.put("countByAlarm", countByAlarm);
        map.put("countByStatus", countByStatus);
        return map;
    }

    @Override
    public List<InspectionRecordDetails> getByRecordId(InspectionRecordDetails inspectionRecordDetails) {
        return baseMapper.selectInspectionRecordDetailsList(inspectionRecordDetails);
    }

    @Override
    public List<InspectionRecordDetails> getNormalByRecordId(InspectionRecordDetails inspectionRecordDetails) {
        LambdaUpdateWrapper<InspectionRecordDetails> wrapper = Wrappers.lambdaUpdate(InspectionRecordDetails.class);
        wrapper.eq(inspectionRecordDetails.getId() != null, InspectionRecordDetails::getRecordId, inspectionRecordDetails.getId());
        wrapper.isNull(InspectionRecordDetails::getAlarmTime);
        wrapper.isNull(InspectionRecordDetails::getAlarmInfo);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<InspectionRecordDetails> getList(InspectionRecordDetails inspectionRecordDetails) {
        LambdaUpdateWrapper<InspectionRecordDetails> wrapper = Wrappers.lambdaUpdate(InspectionRecordDetails.class);
        wrapper.eq(inspectionRecordDetails.getId() != null, InspectionRecordDetails::getRecordId, inspectionRecordDetails.getId());
        return baseMapper.selectList(wrapper);
    }
}
